import User.User;
import User.people;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "dgut_login")
public class dgut_login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
        doDgut_login(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
        doDgut_login(request, response);
    }
    protected void doDgut_login(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
    {
        people p = (people)request.getSession().getAttribute("LOGIN");
        User user = null;
        if(p != null)
        {
            String id = p.getUsername();
            String name = p.getName();
            String passwd = "123456";
            if((user = Treatment.isRight_dgut(id, passwd)) != null)
            {
                request.getSession().setAttribute("SELF", user);
                response.sendRedirect("/user.jsp");
            }
            else if((user = Treatment.writeDgut(name, id, passwd)) != null)
            {
                request.getSession().setAttribute("SELF", user);
                response.sendRedirect("/user.jsp");
            }
            else
            {
                response.sendRedirect("/error.jsp");
            }
        }
    }
}
